package org.lvhengchang.schoolmanager.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lvhengchang.schoolmanager.bean.Student;
import org.lvhengchang.schoolmanager.service.StudentService;
import org.lvhengchang.schoolmanager.service.impl.StudentServiceImpl;

/**
 * 新增学生请求处理类
 * @author LvHengchang
 *
 */
@WebServlet("/student/add")
public class StudentAddServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    	
    	req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
          
        String class_id = req.getParameter("class_id");
    	String student_name = (String) req.getParameter("student_name");
        String student_birthday = (String) req.getParameter("student_birthday");
        String sex = (String) req.getParameter("sex");
        String remarks = (String) req.getParameter("remarks");

        Student student = new Student();
        student.setClass_id(class_id);
        student.setStudent_name(student_name);
        student.setStudent_birthday(student_birthday);
        student.setSex(sex);
        student.setRemarks(remarks);

        try {
            studentService.saveStudent(student);
            req.setAttribute("student_name", student_name);
            req.getRequestDispatcher("/student/student_add_success.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new ServletException(e);
        }

    }

}
